/**
 * 基础函数工具包
 * @param window
 * @param $
 * @param undefined
 */
(function (window, $, undefined) {

	//jQuery扩展
	$.extend($.util, {
		serializeObject: function(formId, flag) {
			var o = {};
			$.each($(formId).serializeArray(), function(index) {
				if(undefined == flag || flag == false) {
					if (this['value'] != undefined && this['value'].length > 0) {
						if (o[this['name']]) {
							o[this['name']] = o[this['name']] + "," + this['value'];
						} else {
							o[this['name']] = this['value'];
						}
					}
				} else {
					if (o[this['name']]) {
						o[this['name']] = o[this['name']] + "," + this['value'];
					} else {
						o[this['name']] = this['value'];
					}
				}
			});
			return o;
		},
		request: function(url) {
			var uri = window.location.search;
			var re = new RegExp("" + url + "=([^&?]*)", "ig");
			return ((uri.match(re)) ? (uri.match(re)[0].substr(url.length + 1)) : null);
		},
		openWin: function(url) {
			var width  = screen.availWidth-10;
			var height = screen.availHeight-50;
			var leftm  = 0;
			var topm   = 0;
			var args = "toolbar=0,location=0,maximize=1,directories=0,status=0,menubar=0,scrollbars=1, resizable=1,left=" + leftm+ ",top=" + topm + ", width="+width+", height="+height;
			var w = window.open(url,"",args);
			if(!w){
				alertify.error('发现弹出窗口被阻止，请更改浏览器设置，以便正常使用本功能！');
				return ;
			}
		}
	});
	
	//解决了IE上js不支持trim的问题
	String.prototype.trim = function() {
		return this.replace(/(^\s*)|(\s*$)/g, "");
	}
	
	//图标
    $.util.namespace("icons");
    window.icons.dialog = function () {
    	var $d =$.easyui.showDialog({
    		content: "<div class=\"easyui-layout\" data-options=\"fit: true\"><div data-options=\"region: 'north', split: false, border: false\" style=\"height: 35px; overflow: hidden;\"><div id='selectIcon'><span id='text'>当前没有选中图标</span><i></i><span id='icon'></span></div></div><div data-options=\"region: 'center', border: false\"><div id='iconsList'></div></div></div>",
    		width: 531, minWidth: 320, height: 380, minHeight: 260,
    		title: "选择图标",
    		iconCls: "icon-hamburg-zoom",
    		maximizable: true,
    		collapsible: true,
    		selected: null,
    		multiple: false,
    		size: null,
    		enableApplyButton: false, enableCloseButton: false, enableSaveButton: false,
    		buttons : [ 
    		           { text : "<i class='fa fa-recycle fa-120 mt-6 fa-c-green'></i> 清除", handler : function() {
    		        	   $("#showIcon").val("");
    		        	   $d.dialog('destroy');
    		           } },
    		           { text : "<i class='fa fa-save fa-120 mt-6 fa-c-blue'></i> 保存", handler : function() {
    		        	   $("#showIcon").val($("#icon").text());
    		        	   $d.dialog('destroy');
    		           } },
    		           { text : "<i class='fa fa-remove fa-120 mt-6 fa-c-red'></i> 关闭", handler : function() { $d.dialog('destroy'); } } 
    		           ]
    	
    	});
    	$.util.exec(function () {
    		var div = $("<div class='icon-box'></div>");
    		var selectIcon = $("#selectIcon") ;
    		for(var i=0;i<iconDatas.length;i++) {
    			var _i = $("<i class='fa "+iconDatas[i]+"' data='fa "+iconDatas[i]+"'></i>");
    			_i.appendTo(div).click(function(){
    				var icon = $(this).attr("data") ;
    				
    				$("#selectIcon").find("i").attr("class", icon);
    				$("#text").html("当前选中的图标值为：") ;
    				$("#icon").html(icon) ;
    				
    				div.children("i").removeClass("selected");
    				$(this).addClass("selected");
    			});
    		}
    		
    		$("#iconsList").html(div);
    	});
    };
    
    window.icons.dialogEasyui = function () {
    	var $d =$.easyui.showDialog({
    		content: "<div class=\"easyui-layout\" data-options=\"fit: true\"><div data-options=\"region: 'north', split: false, border: false\" style=\"height: 35px; overflow: hidden;\"><div id='selectIcon'><span id='text'>当前没有选中图标</span><i></i><span id='icon'></span></div></div><div data-options=\"region: 'center', border: false\"><div id='iconsList'></div></div></div>",
    		width: 531, minWidth: 320, height: 380, minHeight: 260,
            title: "选择图标",
            iconCls: "icon-hamburg-zoom",
            maximizable: true,
            collapsible: true,
            selected: null,
            multiple: false,
            size: null,
            enableApplyButton: false, enableCloseButton: false, enableSaveButton: false,
            buttons : [ 
	          { text : "<i class='fa fa-recycle fa-120 mt-6 fa-c-green'></i> 清除", handler : function() {
	        	   $("#showEasyuiIcon").val("");
	        	   $d.dialog('destroy');
	          } },
              { text : "<i class='fa fa-save fa-120 mt-6 fa-c-blue'></i> 保存", handler : function() {
            	   $("#showEasyuiIcon").val($("#icon").text());
            	   $d.dialog('destroy');
              } },
              { text : "<i class='fa fa-remove fa-120 mt-6 fa-c-red'></i> 关闭", handler : function() { $d.dialog('destroy'); } } 
           	]
    	
        });
    	$.util.exec(function () {
    		var div = $("<div class='icon-box'></div>");
    		var selectIcon = $("#selectIcon") ;
    		console.info(iconDataStandard);
    		$.each(iconDataStandard, function(i, p){
    			console.info(p.iconCls);
    			var _i = $("<i class='"+p.iconCls+"' style='width:35px;height:30px;display:inline-block;' data='"+p.iconCls+"'></i>");
    			_i.appendTo(div).click(function(){
    				var icon = $(this).attr("data") ;
    				
    				$("#selectIcon").find("i").attr("class", icon);
    				$("#text").html("当前选中的图标值为：") ;
    				$("#icon").html(icon) ;
    				
    				div.children("i").removeClass("selected");
    				$(this).addClass("selected");
    			});
    		});
    		$("#iconsList").html(div);
    	});
    };
    
})(window, jQuery);